
package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.WMSOutStockTaskAdapter;
import com.mflex.WHSmartWhouseAd.model.MaterialDetailForDelisModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailInfoForDelisModel;
import com.mflex.WHSmartWhouseAd.model.WHDelisTaskDetailModel;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.List;

public class WMSOutStockTaskActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    EditText txtTaskNo;
    EditText txtEmployee;
    EditText txtVehicleNO;
    ListView txtList;
    MiniLoadingDialog mMiniLoadingDialog;

    EditText txtStorageType;

    String userName;
    String StorageType;

    List<WHDelisTaskDetailModel> WHDelistTaskDetails;
    TaskDetailInfoForDelisModel taskDetailInfoForDelis;
    List<MaterialDetailForDelisModel> MaterialDetailForDelisModel;
//    ScanSmartDataReturnModel scanDataReturnModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_wms_outstock_task);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        txtTaskNo= findViewById(R.id.stvTaskNo);
        txtEmployee= findViewById(R.id.stvEmployee);
        txtVehicleNO= findViewById(R.id.stvVehicleNO);
        txtList= findViewById(R.id.lvList);

        txtStorageType= findViewById(R.id.stvStorageType);

        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        txtTaskNo.setText(intent.getStringExtra("TaskNo"));
        StorageType=intent.getStringExtra("StorageType");
        txtStorageType.setText(StorageType);
//        if (!StringUtils.equals(txtStorageType.toString(),"智能仓"))
//        {
//            txtVehicleNO.isEnabled();
//        }
//        else
//        {
//            txtVehicleNO.isActivated();
//        }

//        Type listType = new TypeToken<List<SubTaskInfoModel>>() {
//        }.getType();
//        WHDelistTaskDetails = (List<WHDelisTaskDetailModel>) JsonUtil.fromJson(intent.getStringExtra("WHDelistTaskDetails"), listType);
//        WMSOutStockTaskAdapter WMSOutStockTaskDetailAdapter = new WMSOutStockTaskAdapter(WMSOutStockTaskActivity.this, WHDelistTaskDetails);
//        txtList.setAdapter(WMSOutStockTaskDetailAdapter);
        inittaskdetail(txtTaskNo.getText().toString());


        txtEmployee.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",txtEmployee.getText().toString()))  {

                        MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描员工号", 1500);
                        txtEmployee.requestFocus();
                        return false;
                    }

                    txtVehicleNO.setText("");
                    txtVehicleNO.requestFocus();
//                    XHttp.get("api/WHSmartWhousePDA/CheckOperator")
//                            .params("strOperator", txtEmployee.getText().toString())
//                            .syncRequest(false)
//                            .onMainThread(true)
//                            .timeOut(120000)
//                            .execute(new SimpleCallBack<Boolean>() {
//                                @Override
//                                public void onSuccess(Boolean response) {
////                                    stvOperator.setText(response);
//                                    PKG.requestFocus();
//                                }
//
//                                @Override
//                                public void onError(ApiException e) {
//                                    mMiniLoadingDialog.dismiss();
//                                    stvOperator.setText("");
//                                    stvOperator.requestFocus();
//                                    MediaPlayer mp = MediaPlayer.create(OutShelfDetailActivity.this, R.raw.error_voice);mp.start();
//                                    new MaterialDialog.Builder(view.getContext())
//                                            .iconRes(R.drawable.icon_warning)
//                                            .title(R.string.tip_warning)
//                                            .content(e.getMessage())
//                                            .positiveText(R.string.lab_submit)
//                                            .show();
//                                }
//                            });
                }
                return false;
            }
        });

        txtVehicleNO.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",txtEmployee.getText().toString())) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描员工号", 1500);
                        txtEmployee.requestFocus();
                        txtVehicleNO.setText("");
                        return false;
                    }
                    if(StringUtils.equals("",txtVehicleNO.getText().toString())) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描载具码", 1500);
                        txtVehicleNO.requestFocus();
                        return false;
                    }
                    XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailInfoForDelistByVehicle")
                            .params("TaskNo", txtTaskNo.getText().toString())
                            .params("VehicleNumber", txtVehicleNO.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<TaskDetailInfoForDelisModel>() {
                                @Override
                                public void onSuccess(TaskDetailInfoForDelisModel response) {

                                    mMiniLoadingDialog.dismiss();
                                    taskDetailInfoForDelis=response;
                                    MaterialDetailForDelisModel=response.getMaterialDetailForDelisModel();

                                    Intent intent = new Intent();
                                    if (StringUtils.equals(StorageType.toString(),"智能仓"))
                                    {
                                         intent = new Intent(WMSOutStockTaskActivity.this, WMSOutStockTaskDetailSmartActivity.class);
                                    }
                                    else {
                                         intent = new Intent(WMSOutStockTaskActivity.this, WMSOutStockTaskDetailActivity.class);
                                    }
                                    intent.putExtra("TaskNo", txtTaskNo.getText().toString());
                                    intent.putExtra("JDEPN", taskDetailInfoForDelis.getJDEPN());
                                    intent.putExtra("TaskType", taskDetailInfoForDelis.getTaskType());
                                    intent.putExtra("MaterialClass", taskDetailInfoForDelis.getMaterialClass());
                                    intent.putExtra("Qty", taskDetailInfoForDelis.getNeedQty());
                                    intent.putExtra("list", JsonUtil.toJson(MaterialDetailForDelisModel));
                                    intent.putExtra("OPT", txtEmployee.getText().toString());
                                    intent.putExtra("TaskDetailID", taskDetailInfoForDelis.getTaskDetailID());
                                    startActivity(intent);

                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });

        txtList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent(WMSOutStockTaskActivity.this, WMSOutStockTaskActivity.class);
//                intent.putExtra("TaskNo", JsonUtil.toJson(materialTaskModelList.get(i).getSubTaskNo()));
//                intent.putExtra("SubTaskInfolist", JsonUtil.toJson(materialTaskModelList.get(i).getSubTaskInfolist()));
//                intent.putExtra("Floor", JsonUtil.toJson(materialTaskModelList.get(i).getFloor()));
//                intent.putExtra("MaterialClass", JsonUtil.toJson(materialTaskModelList.get(i).getMaterialClass()));
//                startActivity(intent);

                if(StringUtils.equals("",txtEmployee.getText().toString()))
               {
                    mMiniLoadingDialog.dismiss();
                    MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                    XToastUtils.success("请先扫描员工号", 1500);
                    txtEmployee.requestFocus();
                    txtVehicleNO.setText("");
                    return;
                }
                XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailInfoForDelistByMaterialPart")
                        .params("TaskNo", txtTaskNo.getText().toString())
                        .params("MaterialPart", WHDelistTaskDetails.get(i).getJDEMaterialPart())
                        .syncRequest(false)
                        .onMainThread(true)
                        .timeOut(120000)
                        .execute(new SimpleCallBack<TaskDetailInfoForDelisModel>() {
                            @Override
                            public void onSuccess(TaskDetailInfoForDelisModel response) {

                                mMiniLoadingDialog.dismiss();
                                taskDetailInfoForDelis=response;
                                MaterialDetailForDelisModel=response.getMaterialDetailForDelisModel();

                                Intent intent = new Intent();
                                if (StringUtils.equals(StorageType.toString(),"智能仓"))
                                {
                                    intent = new Intent(WMSOutStockTaskActivity.this, WMSOutStockTaskDetailSmartActivity.class);
                                }
                                else {
                                    intent = new Intent(WMSOutStockTaskActivity.this, WMSOutStockTaskDetailActivity.class);
                                }
                                intent.putExtra("TaskNo", txtTaskNo.getText().toString());
                                intent.putExtra("JDEPN", taskDetailInfoForDelis.getJDEPN());
                                intent.putExtra("TaskType", taskDetailInfoForDelis.getTaskType());
                                intent.putExtra("MaterialClass", taskDetailInfoForDelis.getMaterialClass());
                                intent.putExtra("Qty", taskDetailInfoForDelis.getNeedQty());
                                intent.putExtra("list", JsonUtil.toJson(MaterialDetailForDelisModel));
                                intent.putExtra("OPT", txtEmployee.getText().toString());
                                intent.putExtra("TaskDetailID", taskDetailInfoForDelis.getTaskDetailID());
                                startActivity(intent);

                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(view.getContext())
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(e.getMessage())
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });
            }
        });

    }
    private void inittaskdetail(String Taskno)
    {

        XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailListForDelist")
                .params("TaskNo", Taskno)
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<List<WHDelisTaskDetailModel>>() {
                    @Override
                    public void onSuccess(List<WHDelisTaskDetailModel> response) {
                        WHDelistTaskDetails=response;
                        mMiniLoadingDialog.dismiss();
                        if(WHDelistTaskDetails.isEmpty())
                        {
                            new MaterialDialog.Builder(WMSOutStockTaskActivity.this)
                                    .iconRes(R.drawable.icon_warning)
                                    .title(R.string.tip_warning)
                                    .content("获取数据为空")
                                    .positiveText(R.string.lab_submit)
                                    .show();
                            return;
                        }
                        WMSOutStockTaskAdapter WMSOutStockTaskDetailAdapter = new WMSOutStockTaskAdapter(WMSOutStockTaskActivity.this, WHDelistTaskDetails);
                        txtList.setAdapter(WMSOutStockTaskDetailAdapter);
                        txtEmployee.setText("");
                        txtEmployee.requestFocus();
                    }

                    @Override
                    public void onError(ApiException e) {

                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(WMSOutStockTaskActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }
    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}



